package cn.hj.domain.activity.service.partake;

import cn.hj.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import cn.hj.domain.activity.model.entity.ActivityEntity;
import cn.hj.domain.activity.model.entity.PartakeRaffleActivityEntity;
import cn.hj.domain.activity.model.entity.UserRaffleOrderEntity;
import cn.hj.domain.activity.model.vo.ActivityStateVO;
import cn.hj.domain.activity.repository.IActivityRepository;
import cn.hj.domain.activity.service.IRaffleActivityPartake;
import cn.hj.types.enums.ResponseCode;
import cn.hj.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 抽奖活动参与
 */
@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartake {


    protected IActivityRepository activityRepository;

    public AbstractRaffleActivityPartake(IActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }


    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {

        //基础信息
        String userId = partakeRaffleActivityEntity.getUserId();
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        Date currentDate = new Date();

        //活动查询
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);
        // 校验；活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 校验；活动日期「开始时间 <- 当前时间 -> 结束时间」
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //查询未使用的活动
        UserRaffleOrderEntity userRaffleOrderEntity = activityRepository.queryNoUsedRaffleOrder(partakeRaffleActivityEntity);
        if (userRaffleOrderEntity != null){
            log.info("创建参与活动订单已存在：{}", userRaffleOrderEntity);
            return userRaffleOrderEntity;
        }

        //账户额度过滤，并且返回账户构建对象
        CreatePartakeOrderAggregate partakeOrderAggregate = doFilterAccount(activityId,userId,currentDate);

        //创建对单
        userRaffleOrderEntity = this.buildUserRaffleOrder(userId, activityId, currentDate);

        //填充抽奖订单实体对象
        partakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrderEntity);

        //保存聚合对象，一个领域内的聚合是一个事务
        activityRepository.saveCreatePartakeOrderAggregate(partakeOrderAggregate);
        return userRaffleOrderEntity;
    }

    @Override
    public UserRaffleOrderEntity createOrder(String userId, Long activityId) {
        PartakeRaffleActivityEntity partakeRaffleActivityEntity = new PartakeRaffleActivityEntity(userId, activityId);
        return createOrder(partakeRaffleActivityEntity);
    }

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);

    protected abstract CreatePartakeOrderAggregate doFilterAccount(Long activityId, String userId, Date currentDate);
}
